package com.bjpowernode.gateway.web.controller;

import com.bjpowernode.gateway.db.domain.PayWay;
import com.bjpowernode.gateway.web.model.dto.PaymentDto;
import com.bjpowernode.gateway.web.model.dto.TradeSelectOrderResultDto;
import com.bjpowernode.gateway.web.model.query.TradeConfirmQuery;
import com.bjpowernode.gateway.web.model.query.TradeReceiveQuery;
import com.bjpowernode.gateway.web.model.query.TradeSelectOrderQuery;
import com.bjpowernode.gateway.web.service.PaymentService;
import com.bjpowernode.gateway.web.service.TradeService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 交易接口
 * private static final Logger log = LoggerFactory.getLogger(LogExample.class);
 */
@Controller
@Slf4j
public class TradeController {

    @Resource
    private TradeService tradeService;

    /**
     * String :  key  对象的名称
     * PaymentService :  业务实现类对象
     */
    @Resource
    private Map<String, PaymentService> paymentServiceMap;

    @GetMapping("/trade/receive")
    public String chackPayInfoReturnPayWay(@Validated TradeReceiveQuery tradeReceiveQuery, HttpServletRequest request, Model model) {
        log.info("测试跳转");
        // 获取请求ip
        String clientIp = request.getRemoteHost();
        // 目前是本机测试 , 故自制一个
        clientIp = "192.0.0.1";
        // 检查并获取数据
        List<PayWay> payWayList = tradeService.chackPayInfoReturnPayWay(tradeReceiveQuery, clientIp);
        model.addAttribute("payway", payWayList);
        model.addAttribute("trade", tradeReceiveQuery);
        // 封装数据
        return "/trade/payways";
    }


    /**
     * 消费者确认支付
     * 判断其 选择的是 快钱支付 还是 微信支付
     * 根据其选择的支付方式  ---> 执行对应的业务逻辑  , 跳转到对应的第三方支付平台  ,  进行支付
     *
     * @return
     */
    @GetMapping("/trade/confirm")
    public String confirmPay(TradeConfirmQuery tradeConfirmQuery, HttpServletRequest request, Model model) {
        log.info("用户支付确认 提交数据 : {}", tradeConfirmQuery);
        // 判断使用的是 哪一个第三方支付接口 :  快钱  | 微信
        String wayAndType = tradeConfirmQuery.getWayAndType();
        String wayCode = wayAndType.split(":")[0];
        // 就是为了进 对应第三方接口的  --->  获取对应的实现类
        // paymentServiceMap :   "KUAIQIAN" -> "xxxxxx"     |    "WEIXIN" ->  "xxxxxx"
        PaymentService paymentService = paymentServiceMap.get(wayCode);
        String remoteHost = request.getRemoteHost();
        tradeConfirmQuery.setMerchIp(remoteHost);
        PaymentDto paymentDto = paymentService.createOrderGenerateApiData(tradeConfirmQuery);
        // 封装到model
        model.addAttribute("pay", paymentDto);
        // 跳到对应的视图上
        return paymentDto.getViewName();
    }

    @PostMapping("/trade/order")
    @ResponseBody
    public TradeSelectOrderResultDto getOrderPayResultInfo(@RequestBody TradeSelectOrderQuery tradeSelectOrderQuery) {
        log.info("商户要查询的订单数据信息 : {}", tradeSelectOrderQuery);
        // 验证签名
        boolean flag = tradeSelectOrderQuery.checkSignMsg();
        flag = true;
        if (!flag) {
            throw new RuntimeException("签名异常");
        }
        // 签名没有问题 , 验证通知参数 ---> 跟新支付结果
        TradeSelectOrderResultDto tradeSelectOrderResultDto = tradeService.checkHandleSelect(tradeSelectOrderQuery);
        return tradeSelectOrderResultDto;
    }
}
